package it.sd.pwframework.admin.controller;

import it.sd.pwframework.business.service.BatchService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
public class StartupListener implements ApplicationListener<ContextRefreshedEvent> {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    private boolean initialized = false;

    @Autowired
    private BatchService batchService;
    
    @Override
    public void onApplicationEvent(ContextRefreshedEvent arg0) {
    	
    	if (!initialized) {
    		
    		log.info("Inizializzazione Startup Listener");
    		
    		try {
        		log.info("initialization service");

        		initService();

        		log.info("Startup Listener inizializzato");

        		initialized = true;

			} catch (Exception e) {
				log.error("Errore inizializzazione startup", e);
			}
    		
    	}
    }

    
    private void initService() throws Exception {

    	batchService.initDb();
    }
    
    
    

}
